#include "VStdAfx.h"
#include "VObject.h"

static VRTTClass* FirstClass = NULL;
typedef vtpl::map<VGuid, VRTTClass*> VObjectClassMap;
static VObjectClassMap* gRTTClassMap = NULL;

VRTTClass::VRTTClass(const VGuid& Guid, VRTTClass* ParentClass, const char* Name, CreateObjectFunc CreateObjFunc, UINT Flags)
	:m_ClassGuid(Guid)
	,m_ParentClass(ParentClass)
	,m_Name(Name)
	,m_CreateObjectFunc(CreateObjFunc)
{
	m_NextClass = FirstClass;
	FirstClass = this;
}

BOOL VRTTClass::InitRTTClasses()
{
	if (gRTTClassMap == NULL)
	{
		gRTTClassMap = new VObjectClassMap;
	}

	gRTTClassMap->clear();

	VRTTClass* RTTClass = FirstClass;
	while(RTTClass)
	{
		const VGuid& ClassGuid = RTTClass->GetClassGuid();
		if (ClassGuid.IsValid())
		{
			VObjectClassMap::iterator itr = gRTTClassMap->find(ClassGuid);
			if (itr != gRTTClassMap->end())
			{
				VERROR("Class Guid Already exist!","");
				return FALSE;
			}
			gRTTClassMap->insert(VObjectClassMap::value_type(ClassGuid, RTTClass));
		}
		
		RTTClass = RTTClass->NextClass();
	}
	return TRUE;
}

void VRTTClass::ShutdownRTTClasses()
{
	if (gRTTClassMap)
	{
		gRTTClassMap->clear();
		delete gRTTClassMap;
		gRTTClassMap = NULL;
	}
}


VObject* VRTTClass::CreateObject(const VGuid& ClassGuid)
{
	VASSERT(gRTTClassMap);
	VObjectClassMap::const_iterator itr = gRTTClassMap->find(ClassGuid);
	if (itr != gRTTClassMap->end())
	{
		VRTTClass* Class = itr->second;
		return Class->CreateObject();
	}
	return NULL;
}


//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
VObject::VObject(void)
{
}


VObject::~VObject(void)
{
}

VObject* VObject::Clone() const
{
	return NULL;
}

void VObject::Copy(VObject* Dest) const
{

}

BOOL VObject::Load(const VXMLElement* Element)
{
	return TRUE;
}

void VObject::Save(VXMLElement* Element)
{

}


VRTTClass* VObject::GetStaticClass() 
{ 
	return &VObject::_RTTClass;
}

VRTTClass* VObject::GetParentStaticClass() 
{
	return NULL;
}
VObject* VObject::_CreateObject()	
{ 
	return NULL; 
}

VRTTClass VObject::_RTTClass(VGuid(VCG_COMMON, 0),NULL,"VObject",VObject::_CreateObject, 0);

const VRTTClass* VObject::GetClass() const
{ 
	return &_RTTClass;
}


